/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package transervfx.Dato.Entidad;

import java.io.Serializable;
import java.util.ArrayList;
import javafx.beans.property.adapter.JavaBeanIntegerProperty;
import javafx.beans.property.adapter.JavaBeanIntegerPropertyBuilder;
import javafx.beans.property.adapter.JavaBeanProperty;
import javafx.beans.property.adapter.JavaBeanStringProperty;
import javafx.beans.property.adapter.JavaBeanStringPropertyBuilder;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
import org.hibernate.validator.constraints.Range;
import transervfx.Dato.Validacion.NoExisteUsuario;
import transervfx.Dato.Validacion.ValidacionesNivelBD;
import transervfx.Dato.Validacion.ValidacionesNivelLogico;

/**
 *
 * @author oscar
 */
@NamedQueries({
    @NamedQuery( name = "Lugar.findAll", query = "SELECT l FROM Lugar l"),
    @NamedQuery( name = "Lugar.count", query = "SELECT COUNT(l) FROM Lugar l "),
    })
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "LUGAR_TIPO")
public abstract class Lugar implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @NotNull(message = "Obligatorio", groups = ValidacionesNivelLogico.class)
    @NotEmpty(message = "Debe completarlo", groups = ValidacionesNivelLogico.class)
    @Size(min = 1, max = 20, message = "1 letras minimos, 12 maximo", groups = ValidacionesNivelLogico.class)
    private String calle;
    @NotNull(message = "Obligatorio", groups = ValidacionesNivelLogico.class)
    @Range(min = 10, max = 9999, message = "1 a 4 digitos ", groups = ValidacionesNivelLogico.class)
    private Integer numero;
    private String departamento;
    private Integer piso;
    @NotNull(message = "debe elegir una localidad", groups = ValidacionesNivelLogico.class)
    @ManyToOne
    private Localidad localidad;
    //-------------------propiedades binding
    @Transient
    private JavaBeanStringProperty PROP_CALLE;
    @Transient
    private JavaBeanIntegerProperty PROP_NUMERO;
    @Transient
    private JavaBeanIntegerProperty PROP_PISO;
    @Transient
    private JavaBeanStringProperty PROP_DEPARTAMENTO;
    @Transient
    private ArrayList<JavaBeanProperty> GroupBindProperties = new ArrayList<JavaBeanProperty>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCalle() {
        return calle;
    }

    public void setCalle(String calle) {
        this.calle = calle;
    }

    public Integer getNumero() {
        return numero;
    }

    public void setNumero(Integer numero) {
        this.numero = numero;
    }

    public Integer getPiso() {
        return piso;
    }

    public void setPiso(Integer piso) {
        this.piso = piso;
    }

    public String getDepartamento() {
        return departamento;
    }

    public void setDepartamento(String departamento) {
        this.departamento = departamento;
    }

    public Localidad getLocalidad() {
        return localidad;
    }

    public void setLocalidad(Localidad localidad) {
        this.localidad = localidad;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Lugar)) {
            return false;
        }
        Lugar other = (Lugar) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    /* @Override
     public String toString() {
     return "aplicaciontranserv.Dato.Entidad.Lugar[ id=" + id + " ]";
     }*/
    //----------------------CODIGO BINDING
    public JavaBeanStringProperty getPROP_CALLE() throws NoSuchMethodException {
        if (PROP_CALLE == null) {
            PROP_CALLE = JavaBeanStringPropertyBuilder.create().bean(this).name("calle").build();
            GroupBindProperties.add(PROP_CALLE);
        }
        return PROP_CALLE;
    }

    public JavaBeanStringProperty getPROP_DEPARTAMENTO() throws NoSuchMethodException {

        if (PROP_DEPARTAMENTO == null) {
            PROP_DEPARTAMENTO = JavaBeanStringPropertyBuilder.create().bean(this).name("departamento").build();
            GroupBindProperties.add(PROP_DEPARTAMENTO);
        }
        return PROP_DEPARTAMENTO;
    }

    public JavaBeanIntegerProperty getPROP_NUMERO() throws NoSuchMethodException {

        if (PROP_NUMERO == null) {
            PROP_NUMERO = JavaBeanIntegerPropertyBuilder.create().bean(this).name("numero").build();
            GroupBindProperties.add(PROP_NUMERO);
        }
        return PROP_NUMERO;
    }

    public JavaBeanIntegerProperty getPROP_PISO() throws NoSuchMethodException {

        if (PROP_PISO == null) {
            PROP_PISO = JavaBeanIntegerPropertyBuilder.create().bean(this).name("piso").build();
            GroupBindProperties.add(PROP_PISO);
        }
        return PROP_PISO;
    }

    public void unbindingALL() {
        for (JavaBeanProperty beanBinding : GroupBindProperties) {
            beanBinding.unbind();
        }
    }

    @Override
    public String toString() {

        return calle+" "+numero+(piso!=0 ? ", 'piso: "+piso+departamento+"', ":"")+", '"+localidad.getNombre()+"', '"+this.getLocalidad().getProvincia().getNombre()+"'";
         
      
    }

    public boolean esSimilar(Lugar lugar) {
       return this.toString().equals(lugar.toString());
        

    }
    
    
}
